package com.front.managementfront.company.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.front.managementfront.company.entity.Company;
import com.front.managementfront.company.service.ICompanyService;
import com.front.managementfront.fegin.IUserAppServiceClient;
import com.front.managementfront.utils.CustomPageUtils;
import com.hkts.management.common.utils.Result;
import com.hkts.management.common.utils.ResultUtil;
import com.hkts.management.common.utils.SnowFlakeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yck
 * @since 2021-05-20
 */
@RestController
@RequestMapping("/front/company")
@Api(tags = "公司管理接口")
public class CompanyController {
    private final Logger logger = LoggerFactory.getLogger(CompanyController.class);

    @Autowired
    public ICompanyService iCompanyService;

    @Autowired
    public IUserAppServiceClient userAppServiceClient;

    /**
     * 分页查询数据
     *
     * @param page    分页信息
     * @param company 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getCompanyPageList")
    public Result<Object> getHontUserList(Page<Company> page, Company company) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iCompanyService.selectPage(page, company)));
    }

    /**
     * 保存和修改公用的
     *
     * @param company 传递的实体
     * @return saveOrUpdateuser AppServiceClient
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/companySave")
    @ApiOperation(value = "公司信息保存")
    public Result<Object> companySave(Company company) {
        company.setDelFlag(0);
        if (StringUtils.isBlank(company.getId())) {
            company.setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()));
        }
        if (StringUtils.isBlank(company.getUserId())) {
            company.setUserId(userAppServiceClient.remoteUserBasicInfo().getResult().getId());
        }
        iCompanyService.saveOrUpdate(company);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/companyGetById")
    public Result<Object> companyGetById(String id) {
        Company byId = iCompanyService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/companyDelete")
    public Result<Object> hontUserDelete(String id) {
        iCompanyService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/companyBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iCompanyService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }

}
